package com.weilan.ark3.read;

import com.weilan.ark3.buffer.DataArray;
import com.weilan.ark3.buffer.MsgQueue;
import com.weilan.ark3.buffer.OverallSituation;
import com.weilan.ark3.entity.ControlDto;
import com.weilan.ark3.entity.ControlRtu;
import com.weilan.ark3.serial.SerialPortService;
import com.weilan.ark3.service.ModbusConfService;
import com.weilan.ark3.tcp.TcpMaster;
import com.weilan.ark3.tcp.TcpSlave;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

import static com.weilan.ark3.tcp.TcpMaster.masterMap;

/**
 * 读取uc数据
 */
@Component
public class ReadRtuData {

    @Autowired
    private SerialPortService serialPortService;


    @Autowired
    private ModbusConfService modbusConfService;
    @Autowired
    private TcpMaster tcpMaster;
    Logger tcplog = LoggerFactory.getLogger("TCPServer_Appender");

    public void ReadRtuData() {
        while (true) {
            //tcplog.info("队列长度" + MsgQueue.com6QueueSize());
            if (MsgQueue.getDueuSize() > 0) {
                ControlDto controlDto = MsgQueue.readFromDueue();
                serialPortService.writeRegister(controlDto);
                //删除历史功率指令
                for (int i = 0; i < MsgQueue.getDueuSize(); i++) {
                    MsgQueue.deleteDueue();
                }
            } else if (MsgQueue.controlQueueSize() > 0) {//普通指令
                ControlDto controlDto = MsgQueue.readFromControlQueue();
                serialPortService.writeRegister(controlDto);
            }
            if (MsgQueue.com6QueueSize() > 0) {
                ControlRtu controlRtu = MsgQueue.readFromCom6Queue();
                //起始地址
                Integer address = controlRtu.getAddress();
                //长度
                Integer len = controlRtu.getLen();
                int slaveId = controlRtu.getSlaveId();
               // Integer offset = controlRtu.getOffset();
                String key = controlRtu.getSlaveId() + ":" + controlRtu.getModuleName() + ":" + controlRtu.getDeviceId() + ":" + controlRtu.getInstructName();

                try {
                    if (controlRtu.getModuleName().equals("ems")) {
                        short[] shorts = Arrays.copyOfRange(DataArray.slaveDataMap.get(1), 0, 99);
                        String emsdate = Arrays.toString(shorts);
                        OverallSituation.CacheData.put(key, emsdate);
//                        continue;
                    } else {
                        //日志记录发送的报文信息
//                    tcplog.info("读取数据的报文:" + "ID:" + controlRtu.getSlaveId() + " " + "address:" + controlRtu.getAddress() + " " + "len:" + controlRtu.getLen());
                        short[] rtuData = serialPortService.readRtuData(controlRtu.getSlaveId(), controlRtu.getAddress(), controlRtu.getLen());
                        if (rtuData != null && rtuData.length == len) {
                            System.arraycopy(rtuData, 0, DataArray.slaveDataMap.get(slaveId), address, len);
                            TcpSlave.slaveMap.get(slaveId).setHoldingRegister(address, rtuData);
                            //将读取的数据转换为字符串
                            String rtuString = Arrays.toString(rtuData);
                            //tcplog.info("COM1接收的报文:" + "ID:" + controlRtu.getSlaveId() + " " + "address:" + controlRtu.getAddress() + " " + "len:" + controlRtu.getLen() + rtuString);
                            //将字符串做截取
                            String data = rtuString.substring(1, rtuString.length() - 1);
                            OverallSituation.CacheData.put(key, data);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    }


    public void ReadTCP1Data(Integer Id) {
        while (true) {
            //数据读取报文(TCP)
            if (MsgQueue.controlTCPSize(Id) > 0) {
                ControlRtu controlRtu = MsgQueue.readFromTCPQueue(Id);
                //设备ID
                Integer slaveId = controlRtu.getSlaveId();
                //起始地址
                Integer address = controlRtu.getAddress();
                //长度
                Integer len = controlRtu.getLen();
                try {
//                    short[] tcpData = deviceDataRead.onModbusSocketRead(slaveId, address, len);
//                    tcplog.info("读取数据的报文:" + "ID:" + controlRtu.getSlaveId() + " " + "address:" + controlRtu.getAddress() + " " + "len:" + controlRtu.getLen());
                    short[] tcpData = tcpMaster.continueReadData(slaveId, address, len, masterMap);
                    if (tcpData != null) {
                        System.arraycopy(tcpData, 0, DataArray.slaveDataMap.get(slaveId), address, len);
                        TcpSlave.slaveMap.get(slaveId).setHoldingRegister(address, tcpData);
                        //将读取的数据转换为字符串
                        String rtuString = Arrays.toString(tcpData);
                        tcplog.info("TCP1接收的报文:" + "ID:" + controlRtu.getSlaveId() + " " + "address:" + controlRtu.getAddress() + " " + "len:" + controlRtu.getLen() + rtuString);
                        String key = controlRtu.getSlaveId() + ":" + controlRtu.getModuleName() + ":" + controlRtu.getDeviceId() + ":" + controlRtu.getInstructName();
                        //将字符串做截取
                        String data = rtuString.substring(1, rtuString.length() - 1);
                        OverallSituation.CacheData.put(key, data);
                        Thread.sleep(100);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void ReadTCP2Data(Integer Id) {
        while (true) {
            //数据读取报文(TCP)
            if (MsgQueue.controlTCPSize(Id) > 0) {
                ControlRtu controlRtu = MsgQueue.readFromTCPQueue(Id);
                //设备ID
                Integer slaveId = controlRtu.getSlaveId();
                //起始地址
                Integer address = controlRtu.getAddress();
                //长度
                Integer len = controlRtu.getLen();
                try {
//                    short[] tcpData = deviceDataRead.onModbusSocketRead(slaveId, address, len);
//                    tcplog.info("读取数据的报文:" + "ID:" + controlRtu.getSlaveId() + " " + "address:" + controlRtu.getAddress() + " " + "len:" + controlRtu.getLen());
                    short[] tcpData = tcpMaster.continueReadData(slaveId, address, len, masterMap);
                    if (tcpData != null) {
                        System.arraycopy(tcpData, 0, DataArray.slaveDataMap.get(slaveId), address, len);
                        TcpSlave.slaveMap.get(slaveId).setHoldingRegister(address, tcpData);
                        //将读取的数据转换为字符串
                        String rtuString = Arrays.toString(tcpData);
                        tcplog.info("TCP2接收的报文:" + "ID:" + controlRtu.getSlaveId() + " " + "address:" + controlRtu.getAddress() + " " + "len:" + controlRtu.getLen() + rtuString);
                        String key = controlRtu.getSlaveId() + ":" + controlRtu.getModuleName() + ":" + controlRtu.getDeviceId() + ":" + controlRtu.getInstructName();
                        //将字符串做截取
                        String data = rtuString.substring(1, rtuString.length() - 1);
                        OverallSituation.CacheData.put(key, data);
                        Thread.sleep(100);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void ReadTCP3Data(Integer Id) {
        while (true) {
            //数据读取报文(TCP)
            if (MsgQueue.controlTCPSize(Id) > 0) {
                ControlRtu controlRtu = MsgQueue.readFromTCPQueue(Id);
                //设备ID
                Integer slaveId = controlRtu.getSlaveId();
                //起始地址
                Integer address = controlRtu.getAddress();
                //长度
                Integer len = controlRtu.getLen();
                try {
//                    short[] tcpData = deviceDataRead.onModbusSocketRead(slaveId, address, len);
//                    tcplog.info("读取数据的报文:" + "ID:" + controlRtu.getSlaveId() + " " + "address:" + controlRtu.getAddress() + " " + "len:" + controlRtu.getLen());
                    short[] tcpData = tcpMaster.continueReadData(slaveId, address, len, masterMap);
                    if (tcpData != null) {
                        System.arraycopy(tcpData, 0, DataArray.slaveDataMap.get(slaveId), address, len);
                        TcpSlave.slaveMap.get(slaveId).setHoldingRegister(address, tcpData);
                        //将读取的数据转换为字符串
                        String rtuString = Arrays.toString(tcpData);
//                        tcplog.info("接收的报文:" + rtuString);
                        String key = controlRtu.getSlaveId() + ":" + controlRtu.getModuleName() + ":" + controlRtu.getDeviceId() + ":" + controlRtu.getInstructName();
                        //将字符串做截取
                        String data = rtuString.substring(1, rtuString.length() - 1);
                        OverallSituation.CacheData.put(key, data);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void ReadTCP4Data(Integer Id) {
        while (true) {
            //数据读取报文(TCP)
            if (MsgQueue.controlTCPSize(Id) > 0) {
                ControlRtu controlRtu = MsgQueue.readFromTCPQueue(Id);
                //设备ID
                Integer slaveId = controlRtu.getSlaveId();
                //起始地址
                Integer address = controlRtu.getAddress();
                //长度
                Integer len = controlRtu.getLen();
                try {
//                    short[] tcpData = deviceDataRead.onModbusSocketRead(slaveId, address, len);
//                    tcplog.info("读取数据的报文:" + "ID:" + controlRtu.getSlaveId() + " " + "address:" + controlRtu.getAddress() + " " + "len:" + controlRtu.getLen());
                    short[] tcpData = tcpMaster.continueReadData(slaveId, address, len, masterMap);
                    if (tcpData != null) {
                        System.arraycopy(tcpData, 0, DataArray.slaveDataMap.get(slaveId), address, len);
                        TcpSlave.slaveMap.get(slaveId).setHoldingRegister(address, tcpData);
                        //将读取的数据转换为字符串
                        String rtuString = Arrays.toString(tcpData);
//                        tcplog.info("接收的报文:" + rtuString);
                        String key = controlRtu.getSlaveId() + ":" + controlRtu.getModuleName() + ":" + controlRtu.getDeviceId() + ":" + controlRtu.getInstructName();
                        //将字符串做截取
                        String data = rtuString.substring(1, rtuString.length() - 1);
                        OverallSituation.CacheData.put(key, data);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void ReadTCP5Data(Integer Id) {
        while (true) {
            //数据读取报文(TCP)
            if (MsgQueue.controlTCPSize(Id) > 0) {
                ControlRtu controlRtu = MsgQueue.readFromTCPQueue(Id);
                //设备ID
                Integer slaveId = controlRtu.getSlaveId();
                //起始地址
                Integer address = controlRtu.getAddress();
                //长度
                Integer len = controlRtu.getLen();
                try {
//                    short[] tcpData = deviceDataRead.onModbusSocketRead(slaveId, address, len);
//                    tcplog.info("读取数据的报文:" + "ID:" + controlRtu.getSlaveId() + " " + "address:" + controlRtu.getAddress() + " " + "len:" + controlRtu.getLen());
                    short[] tcpData = tcpMaster.continueReadData(slaveId, address, len, masterMap);
                    if (tcpData != null) {
                        System.arraycopy(tcpData, 0, DataArray.slaveDataMap.get(slaveId), address, len);
                        TcpSlave.slaveMap.get(slaveId).setHoldingRegister(address, tcpData);
                        //将读取的数据转换为字符串
                        String rtuString = Arrays.toString(tcpData);
//                        tcplog.info("接收的报文:" + rtuString);
                        String key = controlRtu.getSlaveId() + ":" + controlRtu.getModuleName() + ":" + controlRtu.getDeviceId() + ":" + controlRtu.getInstructName();
                        //将字符串做截取
                        String data = rtuString.substring(1, rtuString.length() - 1);
                        OverallSituation.CacheData.put(key, data);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void ReadTCP6Data(Integer Id) {
        while (true) {
            //数据读取报文(TCP)
            if (MsgQueue.controlTCPSize(Id) > 0) {
                ControlRtu controlRtu = MsgQueue.readFromTCPQueue(Id);
                //设备ID
                Integer slaveId = controlRtu.getSlaveId();
                //起始地址
                Integer address = controlRtu.getAddress();
                //长度
                Integer len = controlRtu.getLen();
                try {
//                    short[] tcpData = deviceDataRead.onModbusSocketRead(slaveId, address, len);
//                    tcplog.info("读取数据的报文:" + "ID:" + controlRtu.getSlaveId() + " " + "address:" + controlRtu.getAddress() + " " + "len:" + controlRtu.getLen());
                    short[] tcpData = tcpMaster.continueReadData(slaveId, address, len, masterMap);
                    if (tcpData != null) {
                        System.arraycopy(tcpData, 0, DataArray.slaveDataMap.get(slaveId), address, len);
                        TcpSlave.slaveMap.get(slaveId).setHoldingRegister(address, tcpData);
                        //将读取的数据转换为字符串
                        String rtuString = Arrays.toString(tcpData);
//                        tcplog.info("接收的报文:" + rtuString);
                        String key = controlRtu.getSlaveId() + ":" + controlRtu.getModuleName() + ":" + controlRtu.getDeviceId() + ":" + controlRtu.getInstructName();
                        //将字符串做截取
                        String data = rtuString.substring(1, rtuString.length() - 1);
                        OverallSituation.CacheData.put(key, data);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


